package com.sfdk.admin.ui.components
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import spark.components.Panel;

	public class DraggablePanel extends Panel
	{
		/**
		 *  @private
		 */
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if (instance == titleDisplay)
			{
				titleDisplay.addEventListener(MouseEvent.MOUSE_DOWN, onTitleMouseDown);
				addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			}
		}
		
		/**
		 *  @private
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if (instance == titleDisplay)
			{
				titleDisplay.removeEventListener(MouseEvent.MOUSE_DOWN, onTitleMouseDown);
				removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);				
			}
		}
		
		private function onMouseDown(mouseEvent : MouseEvent) : void
		{
			var localPoint : Point = this.globalToLocal(new Point(mouseEvent.stageX, mouseEvent.stageY));
			if(localPoint.y < 10)//titleDisplay.height)
			{
				addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				startDrag();
			}
		}
		
		private function onMouseUp(mouseEvent : MouseEvent) : void
		{
			removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stopDrag();
		}
		
		private function onTitleMouseDown(mouseEvent : MouseEvent) : void
		{
			titleDisplay.addEventListener(MouseEvent.MOUSE_UP, onTitleMouseUp);
			startDrag();
		}
		
		private function onTitleMouseUp(mouseEvent : MouseEvent) : void
		{
			titleDisplay.removeEventListener(MouseEvent.MOUSE_UP, onTitleMouseUp);
			stopDrag();
		}
	}
	
	
}